<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/gray/window.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css" />
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<ul id="tt"></ul>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
	<script type="text/javascript" src="../../../JS/datagrid-cellediting.js"></script>
	<script type="text/javascript" src="../../../JS/Role/RoleFunction.js"></script>
	<script type="text/javascript">
		var ids = getMultiID();
		var currRoleName = ""; //当前角色名称
		var currRoleID = ids.ID; //当前角色ID
		var updateData = new Object();
		iniUpdateData();
		$(document).ready(function(){
	    	loadRemote();
		});
		var receipts_type = RECEIPT_SAVE_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);

		function save() {
			SaveData();
		}

		function loadRemote() {
			var pLocation = RemoteLinkAddress + '/role/getright.do?ID=' + ids.ID;
			getRemoteData(pLocation);
		}
		function SaveData() {
			var _url = "";
			var checkedTreeRows = $("#tt").tree("getChecked", ['checked', 'indeterminate']);
			var _right = "";
			for(var i = 0; i < checkedTreeRows.length; i++) {
				var s = checkedTreeRows[i];
				_right = _right + s.id + ",";
			}
			updateData[rightList[ids.type]] = _right;
			_url = RemoteLinkAddress + "/role/update.do";
			$.post(_url, updateData,
				function(result) {
					if(result.msg == "success") {
						var parentLocation;
						parentLocation = searchParent("roleManagerList.html");
						parentLocation.dialogClose();
						$.messager.alert({ // show error message
							title: '成功',
							msg: '角色修改成功'
						});
					} else {
						$.messager.alert({ // show error message
							title: '错误',
							msg: "输入的数据已存在数据库"
						});
					}
				},
				'json');
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						console.log(result);
						updateData.ROLE_NAME = result.ROLE_NAME;
						updateData.ID = result.ID;
						handleData(result);
						var _treeData = constructTreeData(result.MENU, rightHASArray[ids.type]);
						$('#tt').tree({
							data: _treeData,
							checkbox: true
						});
					}
				}
			});
		}

		function constructTreeData(_data, type) {
			var _res = new Array();
			var FristLevelArray = $.grep(_data, function(value) {
				return value.PARENT_ID == 0; //筛选出大于5的
			});
			console.log(FristLevelArray);
			for(var i = 0; i < FristLevelArray.length; i++) {
				_res[i] = new Object();
				_res[i].id = FristLevelArray[i].ID;
				_res[i].text = FristLevelArray[i].MENU_NAME;
				_res[i].state="closed";
				//				_res[i].checked = CheckState(FristLevelArray[i], type);
				var SecondLevelArray = $.grep(_data, function(value) {
					return value.PARENT_ID == FristLevelArray[i].ID; //筛选出大于5的
				});
				if(SecondLevelArray.length > 0) {
					_res[i].children = new Array();
					var _second = _res[i].children;
					for(j = 0; j < SecondLevelArray.length; j++) {
						_second[j] = new Object();
						_second[j].id = SecondLevelArray[j].ID;
						_second[j].text = SecondLevelArray[j].MENU_NAME;
						//						_second[j].checked = CheckState(SecondLevelArray[j], type);
						var ThirdLeverArray = $.grep(_data, function(value) {
							return value.PARENT_ID == SecondLevelArray[j].ID; //筛选出大于5的
						});
						if(ThirdLeverArray.length > 0) {
							_second[j].children = new Array();
							var _third = _second[j].children;
							for(m = 0; m < ThirdLeverArray.length; m++) {
								_third[m] = new Object();
								_third[m].id = ThirdLeverArray[m].ID;
								_third[m].text = ThirdLeverArray[m].MENU_NAME;
								_third[m].checked = CheckState(ThirdLeverArray[m], type);
							}
						}

					}
				}
			}
			return _res;
		}

		function CheckState(_data, type) {
			var s;
			for(s in _data) {
				if(s == type) {
					if(_data[s] == true) {
						return true;
					} else {
						return false;
					}
				}
			}
		}

		function handleData(_data) {
			_res = deepCopy(_data.MENU);
			console.log(_res);
			for(var i = 0; i < _res.length; i++) {
				var o = _res[i];
				for(var j = 0; j < rightHASArray.length; j++)
					if(o[rightHASArray[j]] == true) {
						updateData[rightList[j]] = updateData[rightList[j]] + _res[i].ID + ",";
					}
			}
			console.log(updateData);
		}

		function iniUpdateData() {
			for(var j = 0; j < rightList.length; j++) {
				updateData[rightList[j]] = "";
			}
		}
	</script>